<template>
  <div class='login-container'>
    
    <div class="login-box">
      <div class="avatar-box">
        <img src="../assets/swag.jpg" alt=""><!--alt鼠标移动标签-->
      </div>
      <el-form ref="loginformRef" label-width="0px" :model="loginform" :rules="loginformrules" class="login-form el-icon-iconName">
        <!-- 用户 -->
        <el-form-item prop="username">
          
          <el-input prefix-icon="el-icon-user" v-model="loginform.username" placeholder="请输入用户"></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          
          <el-input  prefix-icon="el-icon-lock" v-model="loginform.password" type="password" placeholder="请输入密码"></el-input>
        </el-form-item>
        <!-- 按钮 -->
        <div class="btns">
           <el-button type="primary" @click="login">登录</el-button>
           <el-button type="info" @click="resetloginform">重置</el-button>
        </div>
      </el-form>
    </div>
      
  </div>
</template>

<script>

export default {
    // name:'login',
    data(){
      return{
        //登录表单
        loginform:{
          username:'admin',
          password:'123456'
        },
        //登录验证
        loginformrules:{
          //登录用户名字
          username:[
             { required: true, message: '请输入用户名称', trigger: 'blur' },
            { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
          ],
          //登录用户密码
          password:[
            { required: true, message: '请输入用户密码', trigger: 'blur' },
            { min: 6, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
          ]
        }
      }
    },
    methods:{
      resetloginform() {
        this.$refs.loginformRef.resetFields();
      },
      login(){
        this.$refs.loginformRef.validate(async valid=>{
          if(!valid)return;
          const {data:result}=await this.$http.post("login",this.loginform);
          console.log(result);
          if(result.meta.status!==200)return this.$message.error('登录失败！密码错误');
          this.$message.success('登录成功');
          //保存登录token
          window.sessionStorage.setItem("token",result.data.token);
          this.$router.push("/home");
        });

      }

    }
}
</script>

<style lang="less" scoped>

.login-container{
  background-color: #85FFBD;
background-image: linear-gradient(45deg, #85FFBD 0%, #FFFB7D 100%);



  height: 100%;
  width: 100%;
}
.login-box{
  height: 300px;
  width: 450px;
  border-radius: 10px;
  background-color: #fff;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%,-50%);//位移，第一个是横轴，第二个是纵轴
  .avatar-box{
    height: 130px;
    width: 130px;
    border:1px solid #eee;
    border-radius: 50%;
    box-shadow: 0 0 10px #ddd;
    padding: 10px;
    position: absolute;
    left: 50%;
    transform: translate(-50%,-50%);
    background-color: #fff;
    img{
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #f3eaea;
    }
    
  
    }
    .login-form{
      position: absolute;
      bottom: 0;
      width: 100%;
      padding: 20px;
      box-sizing: border-box;
      
    }
    .btns{
     display: flex;
     justify-content: flex-end;
      
    }
}

</style>
